$(function () {
	/**
	 * 渲染轮播图到指定的元素下
	 * @param {array} arr 录播图图片路径数组
	 */
	function renderInfrared(arr) {
		var e 
		if (arr && arr.length >= 0) {
			//$(e + ' .carousel-indicators').children().remove()
			$('.carousel-inner').children().remove()
			$.each(arr, function (index, item) {
				e = item.isAlarm && item.isAlarm != 'true' ? '.daily' : '.alarm'
				if (index == 0) {
					// $(e + ' .carousel-indicators').append('<li data-target="#carousel-example-alarm" data-slide-to="0" class="active"></li>')
					$(e + ' .carousel-inner').append(`
						<div class="item active">
							<img src=${item.img ? item.img : JSON.parse(item)} alt="...">
							<div class="carousel-caption">
								拍摄时间为: ${item.time}
							</div>
							<div class="row text-c tips">
							  <div class="col-md-6 col-xs-6">最高温度为: ${ item.max ? item.max : '暂无数据' }</div>
							  <div class="col-md-6 col-xs-6">最低温度为: ${ item.min ? item.min : '暂无数据' }</div>
							</div>
						</div>
					`)
				} else {
					// $(e + ' .carousel-indicators').append('<li data-target="#carousel-example-alarm" data-slide-to="'+ index +'" class="active"></li>')
					$(e + ' .carousel-inner').append(`
						<div class="item">
							<img src=${item.img ? item.img : JSON.parse(item)} alt="...">
							<div class="carousel-caption">
								拍摄时间为: ${item.time}
							</div>
							<div class="row text-c tips">
							  <div class="col-md-6 col-xs-6">最高温度为: ${ item.max ? item.max : '暂无数据' }</div>
							  <div class="col-md-6 col-xs-6">最低温度为: ${ item.min ? item.min : '暂无数据' }</div>
							</div>
						</div>
					`)
				}
			})
			if ($('.daily .carousel-inner .item').length == 0) $('.daily').hide()
			if ($('.alarm .carousel-inner .item').length == 0) $('.alarm').hide()
		}
	}
	/**
	 * ajax 请求
	 * @param {object} obj
	 */
	function ajax(obj) {
		obj = obj || {}
		post({
			url: '/Point/PointForDate',
			data: {
				pointId: searchKeyObj['pointid'],
				startTime: obj.startTime || new Date().format(),
				endTime: obj.endTime || new Date().format(),
				type: obj.type || 1,
				pointType: searchKeyObj['type']
			},
			success: (data) => {
				//if (data.returnCode == 200) {
					//data.infrared != null ? renderInfrared('.daily', data.infrared) : $('.daily').hide()
					//data.alarm != null ? renderInfrared('.alarm', data.alarm) : $('.alarm').hide()
				data.infrared != null ?  (data.infrared) : renderInfrared(data)
				//}
			}
		})
	}

	let searchKeyObj = JSON.parse(getLocalStorage('searchKey'))
	let millisecond = 1000 * 60 * 60 * 24
	let currentDate
	function getNextMoth(date) {
		//获得当前月份0-11
		var currentMonth = date.getMonth();
		//获得当前年份4位年
		var currentYear = date.getFullYear();
		//下个月的第一天
		//年份为11代表,是本年的最后一月,所以不能加
		if (currentMonth == 11) {
			currentMonth = 0 //月份为下一年的第一个月份
			currentYear++; //年份加1
		}
		else {
			currentMonth++
		}
		return {
			currentYear,
			currentMonth
		}
	}
	function clickFn(txt, type) {
		let obj = {
			id: type ? '.btns' : null
		}
		switch (txt) {
			case 'day':
				obj.type = 1
				currentDate = new Date()
				obj.startTime = obj.endTime = currentDate.format()
				ajax(obj)
				obj.id ? $(obj.id + ' .btn-group-data-group').hide().siblings('.btn-data').show() : $('.btn-group-data-group').hide().siblings('.btn-data').show()
				break
			case 'week':
				obj.type = 2
				let week = new Date().getDay() != 0 ? new Date().getDay() - 1 : 6
				currentDate = new Date(new Date().getTime() - week * millisecond)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentDate.getTime() + 6 * millisecond).format()
				ajax(obj)
				$(obj.id + ' .btn-group-data-group').hide().siblings('.btn-week').show()
				break
			case 'month':
				obj.type = 3
				var currentMonth = new Date().getMonth()
				//获得当前年份4位年
				var currentYear = new Date().getFullYear()
				//求出本月第一天
				currentDate = new Date(currentYear, currentMonth, 1)
				obj.startTime = currentDate.format()
				//当为12月的时候年份需要加1
				//月份需要更新为0 也就是下一年的第一个月
				//否则只是月份增加,以便求的下一月的第一天
				if (currentMonth == 11) {
					currentYear++;
					currentMonth = 0;
				} else {
					currentMonth++;
				}
				//下月的第一天
				var nextMonthDayOne = new Date(currentYear, currentMonth, 1)
				//求出本月的最后一天
				obj.endTime = new Date(nextMonthDayOne.getTime() - millisecond).format()
				ajax(obj)
				$(obj.id + ' .btn-group-data-group').hide().siblings('.btn-month').show()
				break
			case 'year':
				obj.type = 4
				currentDate = new Date()
				obj.startTime = obj.endTime = currentDate.format()
				ajax(obj)
				$(obj.id + ' .btn-group-data-group').hide().siblings('.btn-year').show()
				break
			case 'propday':
				obj.type = 1
				currentDate = new Date(currentDate - millisecond)
				obj.startTime = obj.endTime = currentDate.format()
				ajax(obj)
				break
			case 'nextday':
				obj.type = 1
				currentDate = new Date(currentDate + millisecond)
				obj.startTime = obj.endTime = currentDate.format()
				ajax(obj)
				break
			case 'propweek':
				obj.type = 2
				currentDate = new Date(currentDate.getTime() - millisecond * 7)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentDate.getTime + millisecond * 6).format()
				ajax(obj)
				break
			case 'nextweek':
				obj.type = 2
				currentDate = new Date(currentDate.getTime() + millisecond * 7)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentDate.getTime + millisecond * 6).format()
				ajax(obj)
				break
			case 'propmonth':
				obj.type = 3
				//获得当前月份0-11
				var currentMonth = currentDate.getMonth();
				//获得当前年份4位年
				var currentYear = currentDate.getFullYear();
				var currentDay = new Date(currentYear, currentMonth, 1);
				//上个月的第一天
				//年份为0代表,是本年的第一月,所以不能减
				if (currentMonth == 0) {
					currentMonth = 11 //月份为上年的最后月份
					currentYear-- //年份减1
				}
				else {
					currentMonth--
				}
				currentDate = new Date(currentYear, currentMonth, 1)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentDay.getTime() - millisecond).format()
				ajax(obj)
				break
			case 'nextmonth':
				obj.type = 3
				var a = getNextMoth(currentDate)
				currentDate = new Date(a.currentYear, a.currentMonth, 1)
				obj.startTime = currentDate.format()
				var b = getNextMoth(currentDate)
				obj.endTime = new Date(new Date(b.currentYear, b.currentMonth, 1).getTime() - millisecond).format()
				ajax(obj)
				break
			case 'propyear':
				obj.type = 4
				var currentYear = currentDate.getFullYear()
				currentYear--
				currentDate = new Date(currentYear, 0, 1)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentYear, 11, 31).format()
				ajax(obj)
				break
			case 'nextyear':
				obj.type = 4
				var currentYear = currentDate.getFullYear()
				currentYear++
				currentDate = new Date(currentYear, 0, 1)
				obj.startTime = currentDate.format()
				obj.endTime = new Date(currentYear, 11, 31).format()
				ajax(obj)
				break
			default:
				break
		}
	}
	/**
	 * 设置页面中 id 为传递的参数的元素的内容为对应 url 参数的值
	 */
	setText('devType')
	setText('device')
	setText('grade')
	setText('devName')
	setText('position')
	clickFn('day')
	//renderInfrared('.daily', arr1)
	//renderInfrared('.alarm', arr2)

	$(document).on('click', '.btn', function () {
		var txt = $(this).data('text')
		var type = $(this).data('type')
		clickFn(txt, type)
	})
})